// 全局共享数据示例
import { useState } from 'react';
import { ConfigProvider } from 'antd';
import zhCN from 'antd/locale/zh_CN';
import enUS from 'antd/locale/en_US';
import dayjs from 'dayjs';

const useGlobal = () => {
  const [locale, setLocale] = useState<'zh-CN' | 'en-US'>('zh-CN');
  const [primaryColor, setPrimaryColor] = useState('#1890ff');

  const handleLocaleChange = (value: 'zh-CN' | 'en-US') => {
    setLocale(value);
    // 设置 dayjs 语言
    dayjs.locale(value === 'zh-CN' ? 'zh-cn' : 'en');
    // 设置 antd 语言
    ConfigProvider.config({
      locale: value === 'zh-CN' ? zhCN : enUS,
    });
  };

  const handlePrimaryColorChange = (color: string) => {
    setPrimaryColor(color);
    ConfigProvider.config({
      theme: {
        token: {
          colorPrimary: color,
        },
      },
    });
  };

  return {
    locale,
    setLocale: handleLocaleChange,
    primaryColor,
    setPrimaryColor: handlePrimaryColorChange,
  };
};

export default useGlobal;
